﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace WeiMiao
{
    public class Pagoda : MonoBehaviour
    {
        public string objectTag;

        Ray ray; // 检测射线

        public GameObject Location;//位置

        public int num;

        public List<GameObject> games;
        
        // Start is called before the first frame update
        void Start()
        {
        
        }

        // Update is called once per frame
        void Update()
        {
            CastObject();
        }

        // 物体检测函数
        void CastObject()
        {
            // 设置射线
            ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            // 判断点击
            if (Input.GetMouseButtonDown(0))
            {
                RaycastHit hit;

                // 射线检测
                if (Physics.Raycast(ray, out hit))
                {

                    if (hit.transform.tag == objectTag)
                    {
                        bool click = hit.transform.gameObject.GetComponent<PagodaCube>().click;
                        if (click)
                        {
                            games.Remove(hit.transform.gameObject);
                            hit.transform.gameObject.GetComponent<PagodaCube>().click = false;
                            hit.transform.localPosition = hit.transform.gameObject.GetComponent<PagodaCube>().primary_position;

                        }
                        else
                        {
                            games.Add(hit.transform.gameObject);
                            hit.transform.gameObject.GetComponent<PagodaCube>().primary_position = hit.transform.localPosition;
                            hit.transform.gameObject.GetComponent<PagodaCube>().click = true;
                            hit.transform.position = new Vector3(Location.transform.position.x, Location.transform.position.y + 10, Location.transform.position.z);
                        }
                        if(games.Count == num)
                        {
                            int forint = 0;
                            bool clearance = true;
                            foreach (var item in games)
                            {
                                forint++;
                                if(item.GetComponent<PagodaCube>().size_ranking != forint)
                                {
                                    clearance = false;
                                }
                            }
                            if (clearance)
                            {
                                GameObject.Find("OverallUI").gameObject.GetComponent<UI>().Pass();
                            }
                            else
                            {
                                Debug.Log("通关失败");
                            }
                        }
                    }
                }
            }
        }
    }
}
